unit dGetIP;

interface
 uses Classes,
      SysUtils,
      Winsock;

 function GetIp: string;
 function GetLocalIP(ip : integer=1) : string;

implementation


function GetIp: string;
  var
           WSAData: TWSAData;
           HostEnt: PHostEnt;
           Name:string;
           Retorno : string;
    begin

    Retorno := '';

    try
           WSAStartup(2, WSAData);
           SetLength(Name, 255);
           Gethostname(PChar(Name), 255);
           SetLength(Name, StrLen(PChar(Name)));
           HostEnt := gethostbyname(PChar(Name));
           with HostEnt^  do begin
                    Retorno := Format('%d.%d.%d.%d',[Byte(h_addr^[0]),Byte(h_addr^[1]),Byte(h_addr^[2]),Byte(h_addr^[3])]);
            end;

   finally

         WSACleanup;

   end;

   result := Retorno + ':' + Name;

end;



function GetLocalIP(ip : integer=1) : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
AHost : string;
begin

  WSAStartup($101, GInitData);

  Result := '';

  GetHostName(Buffer, SizeOf(Buffer));

  phe :=GetHostByName(buffer);

  if phe = nil then Exit;

  AHost := Phe^.h_name;

  if (Phe^.h_aliases) <> nil then
  begin

   AHost := IntToStr( Phe^.h_addrtype );

   AHost := IntToStr( Phe^.h_length );



  end;



  pptr := PaPInAddr(Phe^.h_addr_list);



  I := 0;

  while pptr^[I] <> nil do
  begin

   if (ip > 1) then
   begin
   if result <> '' then
   result := result + '; ' + StrPas(inet_ntoa(pptr^[I]^))
   else
   result := StrPas(inet_ntoa(pptr^[I]^));
   end
   else
    result := StrPas(inet_ntoa(pptr^[0]^));



   Inc(I);

  end;

  WSACleanup;

end;




end.
